<template>
  <div class="node-container" :class="{ selected: selected, round: round }">
    <div class="svg-container" :style="{ backgroundColor: color }">
      <svg-icon :icon-class="icon" style="color: #fff;font-size:20px"></svg-icon>
    </div>
    <div :title="title" class="node-text">{{ title }}</div>
  </div>
</template>

<script>
import { fromEventPattern } from "rxjs";
export default {
  name: "testnode",
  directives: {},
  inject: ["getGraph", "getNode"],
  props: {
    round: {
      //是否为圆角
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      selected: false,
      icon: "",
      color: "",
      title:''
    };
  },
  created() {
    this.getData();
  },
  mounted() {
    this.init();
  },
  methods: {
    getData() {
      this.icon = this.getNode().data
        ? this.getNode().data.icon || this.getNode().data.node.icon || ""
        : "";
      this.color = this.getNode().data
        ? this.getNode().data.color || this.getNode().data.node.color || ""
        : "";
        this.title=this.getNode().data.text||this.getNode().attrs.label.text
    },
    init() {
      const graph = this.getGraph();
      // 处理节点选中事件
      fromEventPattern(
        (handler) => {
          graph.on("selection:changed", handler);
        },
        (handler) => {
          graph.off("selection:changed", handler);
        }
      ).subscribe((args) => {
        const { removed, selected } = args;

        this.setCellsSelectedStatus(removed, false);
        this.setCellsSelectedStatus(selected, true);
      });

      graph.on('cell:change:data', (args) => {
        if (args.cell.id === this.getNode().id) {
          this.title=args.cell.data.text||this.getNode().attrs.label.text
        }
      })
      graph.on('cell:change:attrs', (args) => {
        if (args.cell.id === this.getNode().id) {
          //this.title=args.cell.data.text||this.getNode().data.displayName
        }
      })
   
      

    },
    setCellsSelectedStatus(cells, selected) {
      cells.forEach((cell) => {
        const data = cell.getData() || {};
        cell.setData({ ...data, selected });
        if (cell.id === this.getNode().id) {
          this.selected = selected;
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.svg-container {
  display: inline-flex;
  flex-grow: 0;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background-color: rgba(229, 238, 255, 0.85);
  border-radius: 4px 0 0 4px;
}

.node-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  overflow: hidden;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
  box-sizing: border-box;

  &.selected,
  &:hover {
    background-color: rgba(243, 249, 255, 0.92);
    border: 1px solid #1890ff;
    box-shadow: 0 0 3px 3px rgba(64, 169, 255, 0.2);
  }

  .text {
    color: rgba(0, 0, 0, 0.65);
    font-size: 12px;
    white-space: nowrap;
    text-overflow: ellipsis;
    vertical-align: middle;
    //  padding: 0 8px;
    z-index: 1;
  }
}

.round {
  border-radius: 20px;
  height: 36px;

  .svg-container {
    display: inline-flex;
    flex-grow: 0;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    margin-left: 4px;
  }
}

.node-text {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100px;
  margin-left: 4px;
  text-align:center;

}
</style>